package com.ftm.service.impl;

import java.util.List;

import com.ftm.dao.impl.GoodsDaoImpl;
import com.ftm.dao.impl.ShopCarDaoImpl;
import com.ftm.entity.Goods;
import com.ftm.entity.Shop;
import com.ftm.entity.Shopc;
import com.ftm.service.GoodsService;
import com.ftm.tools.DBTools;
import com.ftm.tools.PageData;

/**
 * 商品业务
 * 
 * @author zcl
 *
 */
public class GoodsServiceImpl implements GoodsService {
	GoodsDaoImpl gd = new GoodsDaoImpl();

	/**
	 * 分页查询全部商品/测试完成
	 */
	@Override
	public PageData<Goods> showGoods(int page, int pageSize, String keywords) {
		if (page < 1) {
			page = 1;
		} else if (page > pageSize) {
			page = pageSize;
		}
		PageData<Goods> pd = gd.queryGoods(page, pageSize, keywords);
		return pd;
	}

	/**
	 * 添加商品进入购物车
	 */
	@Override
	public boolean addShopc(Shopc shopc) {
		ShopCarDaoImpl sc = new ShopCarDaoImpl();
		boolean flag = sc.addShopCar(shopc);
		if (flag)
			// 添加成功
			return true;
		return false;
	}

	@Override
	public Goods selShopName(Goods goods, String goodsname) {
		String sql = "select * from ftm_shop where shopid=(select shopid from ftm_goods where goodsname=?)";
		List<Shop> list = (List<Shop>) DBTools.exQuery(sql, Shop.class, goodsname);
		if (list.size() == 0) {
			return null;
		}
		Goods g = new Goods(goods.getGoodsid(), goods.getGoodsname(), goods.getGoodsprice(), goods.getGoodsinfo(),
				goods.getGoodscount(), goods.getCover(), goods.getTypeid(), goods.getTypeid(),
				list.get(0).getShopname());
		return g;
	}
	
	@Override
	public PageData<Goods> queryGoodsShopid(int page, int pageSize, int shopid) {
		if (page < 1) {
			page = 1;
		} else if (page > pageSize) {
			page = pageSize;
		}
		return gd.queryGoodsShopid(page, pageSize, shopid);
	}

	@Override
	public List<Goods> queryAllGoods() {
		List<Goods> list =gd.queryAllGoods();
		if(list==null) {
			return null;
		}
		return list;
	}

	@Override
	public boolean setGoodsState(int id, int state) {
		boolean f=gd.updateGoods(id, state);
		return f;
	}
}
